package com.example.foodtruckdemo.util;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * ClassName: GpsDistanceUtils
 * CreateUser: vayne
 * CreateTime: 2021/7/29
 */
public class GpsDistanceUtils {

    /**
     * 根据Wgs84坐标/经纬度计算两目标位置的直线距离
     *
     * @param lonA 目标A经度
     * @param latA 目标A纬度
     * @param lonB 目标B经度
     * @param latB 目标B纬度
     * @param newScale 结果保留小数位
     * @return 距离 （m）
     */
    public static double getDistance(Double lonA, Double latA, Double lonB, Double latB,int newScale) {
        GlobalCoordinates source = new GlobalCoordinates(latA, lonA);
        GlobalCoordinates target = new GlobalCoordinates(latB, lonB);
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target);
        double distance = geoCurve.getEllipsoidalDistance();
        BigDecimal distanceBig = new BigDecimal(distance).setScale(newScale, RoundingMode.UP);
        return distanceBig.doubleValue();
    }

}